﻿namespace WoJ_UT_Mike
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    using DeltaGroup.WheelOfJeopardy.ChallengeManager;
    using DeltaGroup.WheelOfJeopardy.Model;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    ///This is a test class for PhraseTest and is intended
    ///to contain all PhraseTest Unit Tests
    ///</summary>
    [TestClass]
    public class PhraseTest
    {
        #region Fields

        private TestContext testContextInstance;

        #endregion Fields

        #region Properties

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// A test for Contains
        /// </summary>
        [TestMethod]
        public void ContainsTest()
        {
            string phraseString = "Who is the Delta Group?";
            Phrase target = new Phrase(phraseString, StopWords.BaseWordList);
            string token1 = "Delta";
            bool expected1 = true;
            bool actual1;

            actual1 = target.Contains(token1);
            Assert.AreEqual(expected1, actual1);

            string token2 = "Group";
            bool expected2 = true;
            bool actual2;

            actual2 = target.Contains(token2);
            Assert.AreEqual(expected2, actual2);

            string token3 = "the";
            bool expected3 = false;
            bool actual3;

            actual3 = target.Contains(token3);
            Assert.AreEqual(expected3, actual3);
        }

        /// <summary>
        ///A test for Contains
        ///</summary>
        [TestMethod]
        public void ContainsTestWithSynList()
        {
            string phraseString = "Who is the Delta Group Widget?";
            List<Synonym> synonymList = new List<Synonym>();
            synonymList.Add(new ConstructableSynonym("Delta", "Change"));
            synonymList.Add(new ConstructableSynonym("Group", "Team"));
            synonymList.Add(new ConstructableSynonym("Group", "Unit"));

            Phrase target = new Phrase(phraseString, new List<string>());
            string[] tokens = { "Delta", "Change", "Group", "Team", "Unit", "Widget" };
            IEqualityComparer<string> comparer = new SynonymComparer(synonymList);
            bool actual;
            foreach (string token in tokens)
            {
                actual = target.Contains(token, comparer);
                Assert.IsTrue(actual);
            }

            actual = target.Contains("River", comparer);
            Assert.IsFalse(actual);
        }

        /// <summary>
        ///A test for System.Collections.Generic.IEqualityComparer<DeltaGroup.WheelOfJeopardy.ChallengeManager.Phrase>.Equals
        ///</summary>
        [TestMethod]
        [DeploymentItem("DeltaGroup.WheelOfJeopardy.exe")]
        public void EqualsTest()
        {
            string phraseString = "Who is the Delta Group?";
            Phrase target1 = new Phrase(phraseString, new List<string>());
            Phrase target2 = new Phrase(phraseString, new List<string>());
            IEqualityComparer<Phrase> iec = target1 as IEqualityComparer<Phrase>;
            Assert.IsTrue(iec.Equals(target1, target2));
        }

        /// <summary>
        ///A test for System.Collections.Generic.IEqualityComparer<DeltaGroup.WheelOfJeopardy.ChallengeManager.Phrase>.GetHashCode
        ///</summary>
        [TestMethod]
        [DeploymentItem("DeltaGroup.WheelOfJeopardy.exe")]
        public void GetHashCodeTest()
        {
            string phraseString = "Who is the Delta Group?";
            string phraseString2 = "Who is the Gamma Group?";

            Phrase obj = new Phrase(phraseString, new List<string>());
            IEqualityComparer<Phrase> target = obj as IEqualityComparer<Phrase>;
            int actual;
            int actual2;
            actual = target.GetHashCode(obj);

            Phrase obj2 = new Phrase(phraseString2, new List<string>());
            actual2 = ((IEqualityComparer<Phrase>)obj2).GetHashCode(obj2);

            Assert.AreNotEqual<int>(actual2, actual);
        }

        /// <summary>
        ///A test for System.Collections.Generic.IEnumerable<System.String>.GetEnumerator
        ///</summary>
        [TestMethod]
        [DeploymentItem("DeltaGroup.WheelOfJeopardy.exe")]
        public void IteratorTest()
        {
            string phraseString = "WordA WordB WordC WordD WordE WordF WordG";
            string[] phraseStringArray = phraseString.Split(new char[] { ' ' });
            Phrase phrase = new Phrase(phraseString, new List<string>());
            IEnumerator<string> actual = ((IEnumerable<string>)phrase).GetEnumerator();
            Assert.IsNotNull(actual);

            int expectedIterations = 7;
            int actualIterations = 0;

            foreach (string token in phrase)
            {
                actualIterations++;
            }

            Assert.AreEqual(expectedIterations, actualIterations);

            //does it reset
            int actualIterations2 = 0;
            foreach (string token in phrase)
            {
                actualIterations2++;
            }
            Assert.AreEqual(expectedIterations, actualIterations);

            //does it reset
            int index = 0;
            foreach (string token in phrase)
            {
                Assert.AreEqual(phraseStringArray[index++], token);
            }
        }

        /// <summary>
        ///A test for Phrase Constructor
        ///</summary>
        [TestMethod]
        public void PhraseConstructorTest()
        {
            string phraseString = "Who is the Delta Group?";
            Phrase target = new Phrase(phraseString, new List<string>());
            Assert.AreEqual(phraseString, target.ToString());
        }

        /// <summary>
        ///A test for TokenCount
        ///</summary>
        [TestMethod]
        public void TokenCountTestStrict()
        {
            string phraseString = "What is the Delta Group about?";
            Phrase target = new Phrase(phraseString, StopWords.BaseWordList);
            int expected = 4;
            int actual;
            actual = target.TokenCount;
            Assert.AreEqual<int>(expected, actual);
        }

        /// <summary>
        ///A test for TokenCount
        ///</summary>
        [TestMethod]
        public void TokenCountTestModerate()
        {
            string phraseString = "What is the Delta Group about?";
            Phrase target = new Phrase(phraseString, StopWords.ModerateWordList);
            int expected = 3;
            int actual;
            actual = target.TokenCount;
            Assert.AreEqual<int>(expected, actual);
        }

        /// <summary>
        ///A test for TokenCount
        ///</summary>
        [TestMethod]
        public void TokenCountTestRelaxed()
        {
            string phraseString = "What is the Delta Group about?";
            Phrase target = new Phrase(phraseString, StopWords.RelaxedWordList);
            int expected = 2;
            int actual;
            actual = target.TokenCount;
            Assert.AreEqual<int>(expected, actual);
        }
        #endregion Methods
    }
}